package com.yitongmao.fragment;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

import com.google.gson.Gson;
import com.yitongmao.AppConstancts;
import com.yitongmao.adapter.DaiShouhuoAdapter;
import com.yitongmao.bean.DaiShouhuoBean;
import com.yitongmao.bean.LoginHuancunBean;
import com.yitongmao.common.CallBackListener;
import com.yitongmao.core.cache.ACache;
import com.yitongmao.core.network.service.BaseService;
import com.yitongmao.util.SignUtil;
import com.yitongmao.util.ToastUtils;
import com.yitongmao.yitmfruit.R;

import java.util.Map;
import java.util.Random;
import java.util.TreeMap;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * A simple {@link Fragment} subclass.
 */
public class ShouHuoFragment extends Fragment {
    @Bind(R.id.mAllListView)
    ListView mAllListView;
    private LoginHuancunBean loginHuancunBean;
    private int loginId1;

    public ShouHuoFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_shou_huo, container, false);
        Gson gson = new Gson();
        String loginId = ACache.get(getActivity()).getAsString("loginId");
        if (loginId != null) {
            loginHuancunBean = gson.fromJson(loginId, LoginHuancunBean.class);
            int id = loginHuancunBean.userInfo.id;
            loginId1 = loginHuancunBean.userInfo.loginId;
            setAllList(id);
        } else {
            ToastUtils.showShort(getActivity(), "还没有登录哟！");
        }
        ButterKnife.bind(this, view);
        return view;
    }
    private void setAllList(int userId) {
        Map params = new TreeMap();
        String timestamp = String.valueOf(System.currentTimeMillis());
        String nonce = String.valueOf(new Random().nextInt());
        params.put("timestamp", timestamp);
        params.put("userId",String.valueOf(userId));
        params.put("state","03");//00-未付款;02-待发货;03-待收货;04-已完成;05-已取消;06-已退款
        params.put("nonce", nonce);
        params.put("signLogin", SignUtil.signLogin(nonce, timestamp));
        params.put("loginId", String.valueOf(loginId1));
        params.put("sign", SignUtil.sign(params)); //这里也需要时完整的url
        BaseService.getInstance().simplePostRequest(AppConstancts.Net.allListUrl, params, new CallBackListener() {
            @Override
            public void onStart() {
            }

            @Override
            public void onSuccess(Object result) {
                String string = (String) result;
                Gson gson = new Gson();
                DaiShouhuoBean daiShouhuoBean = gson.fromJson(string, DaiShouhuoBean.class);
                ToastUtils.showShort(getActivity(),""+daiShouhuoBean.result.message);
                if (daiShouhuoBean.result.state.equals("1")){
                    if (daiShouhuoBean.orderList != null) {
                        mAllListView.setAdapter(new DaiShouhuoAdapter(getActivity(), daiShouhuoBean.orderList));
                    }
                }
            }

            @Override
            public void onFail(Object result) {
                System.out.print("失败");
                ToastUtils.showShort(getActivity(), "失败！");
            }
        });
    }

}
